# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=setuptools
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
           "${MINGW_PACKAGE_PREFIX}-python2-setuptools<42.0.2")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=47.1.1
pkgrel=1
pkgdesc="Easily download, build, install, upgrade, and uninstall Python packages (mingw-w64)"
arch=('any')
license=('PSF')
url="https://pypi.python.org/pypi/setuptools"
depends=("${MINGW_PACKAGE_PREFIX}-python>=3.3"
         "${MINGW_PACKAGE_PREFIX}-python-packaging"
         "${MINGW_PACKAGE_PREFIX}-python-pyparsing"
         "${MINGW_PACKAGE_PREFIX}-python-ordered-set"
         "${MINGW_PACKAGE_PREFIX}-python-appdirs"
         "${MINGW_PACKAGE_PREFIX}-python-six")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-certifi: Mozilla's CA Bundle support"
            "${MINGW_PACKAGE_PREFIX}-python-wincertstore: Windows' cert store support")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-certifi"
             "${MINGW_PACKAGE_PREFIX}-python-wincertstore"
             'git')
install=${_realname}3-${CARCH}.install
source=(${_realname}-${pkgver}.tar.gz::https://github.com/pypa/setuptools/archive/v${pkgver}.tar.gz
        '0001-mingw-python-fix.patch'
        '0002-Allow-usr-bin-env-in-script.patch'
        '0003-MinGW-w64-Look-in-same-dir-as-script-for-exe.patch'
        '0005-execv-warning.patch')
#_checkdeps=('mock' 'pytest-flake8' 'virtualenv' 'pytest-virtualenv' 'pytest'
#            'wheel' 'coverage' 'pytest-cov')
#checkdepends=("${_checkdeps[@]/#/${MINGW_PACKAGE_PREFIX}-python3-}"
#              "${MINGW_PACKAGE_PREFIX}-python3-paver"
#              'git')
sha256sums=('e0cfb23178cb2d1d06a17567c81c47d3bfd3a881ca02e988bf990b61d871109b'
            'd3bc778723e63bbd6e43ab3536a015c547c8c20a95c6679a952284a7a4822996'
            '7bb5617c69566f5fbf1a3ee29a08179e1b41bdeabbc2998bf67615e9aa000424'
            '0e556505cb70ff3a5df856e352d5e2b3cf1582c25d02fc00a2d935a28576b28c'
            'f8db482721900dbbfd19f52b9042954c85b9e3a61a5e4b68a82aeafec8cba34a')

prepare() {
  export SETUPTOOLS_INSTALL_WINDOWS_SPECIFIC_FILES=1
  cd "${srcdir}/setuptools-${pkgver}"
  patch -p1 -i ${srcdir}/0001-mingw-python-fix.patch
  patch -p1 -i ${srcdir}/0002-Allow-usr-bin-env-in-script.patch
  patch -p1 -i ${srcdir}/0003-MinGW-w64-Look-in-same-dir-as-script-for-exe.patch
  patch -p1 -i ${srcdir}/0005-execv-warning.patch

  cd "${srcdir}"

  # Compile our own MSYS2-layout and /usr/bin/env capable {cli,gui}{-32,-64}.exe to replace the precompiled binaries.
  # .. when arm is ready, add it to this.
  PATH=/mingw32/bin:$PATH gcc -DGUI=0           -O -s -o setuptools-${pkgver}/setuptools/cli-32.exe setuptools-${pkgver}/launcher.c
  PATH=/mingw32/bin:$PATH gcc -DGUI=1 -mwindows -O -s -o setuptools-${pkgver}/setuptools/gui-32.exe setuptools-${pkgver}/launcher.c
  PATH=/mingw64/bin:$PATH gcc -DGUI=0           -O -s -o setuptools-${pkgver}/setuptools/cli-64.exe setuptools-${pkgver}/launcher.c
  PATH=/mingw64/bin:$PATH gcc -DGUI=1 -mwindows -O -s -o setuptools-${pkgver}/setuptools/gui-64.exe setuptools-${pkgver}/launcher.c

  rm -r setuptools-${pkgver}/{pkg_resources,setuptools}/{extern,_vendor}

  # Upstream devendoring logic is badly broken, see:
  # https://bugs.archlinux.org/task/58670
  # https://github.com/pypa/pip/issues/5429
  # https://github.com/pypa/setuptools/issues/1383
  # The simplest fix is to simply rewrite import paths to use the canonical
  # location in the first place
  for _module in setuptools pkg_resources '' ; do
     find setuptools-${pkgver}/${_module} -name \*.py -exec sed -i \
         -e 's/from '${_module}.extern' import/import/' \
         -e 's/from '${_module}.extern'./from /' \
         -e 's/import '${_module}.extern'./import /' \
         -e "s/__import__('$_module.extern./__import__('/" \
         {} +
   done

  # Remove post-release tag since we are using stable tags
  sed -e '/tag_build = .post/d' \
      -e '/tag_date = 1/d' \
      -i setuptools-${pkgver}/setup.cfg

  # 'Clean' installation is expected to fail since we removed bundled packages
  #sed -i '/^def test_clean_env_install/i import pytest\n\n@pytest.mark.xfail' setuptools-${pkgver}/setuptools/tests/test_virtualenv.py

  # Tests failed. Importing an unbundled new setuptools in a virtualenv does not work, but this won't
  # affect normal virtualenv usage (which don't have to import the unbundled setuptools in *current*
  # dir.
  sed -e '/^def test_pip_upgrade_from_source/i @pytest.mark.xfail' \
      -e '/^def test_test_command_install_requirements/i @pytest.mark.xfail' \
      -e '/^def test_no_missing_dependencies/i @pytest.mark.xfail' \
      -i setuptools-${pkgver}/setuptools/tests/test_virtualenv.py

  cp -r setuptools-${pkgver} setuptools-python-${CARCH}

  cd "${srcdir}"/setuptools-python-${CARCH}
  sed -i -e "s|^#\!.*/usr/bin/env python|#!/usr/bin/env python|" setuptools/command/easy_install.py
}

build() {
  # Build python  module
  cd "${srcdir}"/setuptools-python-${CARCH}
  ${MINGW_PREFIX}/bin/python bootstrap.py
  ${MINGW_PREFIX}/bin/python setup.py build
  rm -rf build/lib/pkg_resources/_vendor
  rm -rf build/lib/setuptools/_vendor
}

#check() { 
#  # Workaround UTF-8 tests by setting LC_CTYPE
#  export LC_CTYPE=en_US.UTF-8

#  # https://github.com/pypa/setuptools/pull/810
#  export PYTHONDONTWRITEBYTECODE=1

#  cd "${srcdir}"/setuptools-python-${CARCH}
#  ${MINGW_PREFIX}/bin/python setup.py pytest
#}

package() {
  local _mingw_prefix=$(cygpath -wm ${MINGW_PREFIX})

  cd "${srcdir}/setuptools-python-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build

  sed -e "s|${_mingw_prefix}/bin/|${MINGW_PREFIX}/bin/|g" -i ${pkgdir}${MINGW_PREFIX}/bin/easy_install-script.py
}
